package com.sc.study.foundation.controller;

import java.util.*;
import javax.servlet.http.HttpServletResponse;

import com.sc.common.core.domain.entity.SysUser;
import com.sc.common.utils.DateUtils;
import com.sc.study.clazz.domain.ScClazzSubjectTeacher;
import com.sc.study.foundation.domain.ScSubjectTableVo;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.sc.common.annotation.Log;
import com.sc.common.core.controller.BaseController;
import com.sc.common.core.domain.AjaxResult;
import com.sc.common.enums.BusinessType;
import com.sc.study.foundation.domain.ScSubjectTable;
import com.sc.study.foundation.service.IScSubjectTableService;
import com.sc.common.utils.poi.ExcelUtil;
import com.sc.common.core.page.TableDataInfo;

/**
 * 课程Controller
 * 
 * @author sc
 * @date 2022-02-27
 */
@RestController
@RequestMapping("/study/subjecttable")
public class ScSubjectTableController extends BaseController
{
    @Autowired
    private IScSubjectTableService scSubjectTableService;


    /**
     * 查询能排课的授课班级以及授课老师信息
     */
    @PreAuthorize("@ss.hasPermi('study:subjecttable:subject_clazz_teacher')")
    @GetMapping("/selectSubjectClazzTeacher")
    public AjaxResult selectSubjectClazzTeacher()
    {
        List<Map> maps = scSubjectTableService.selectSubjectClazzTeacher();
        return AjaxResult.success(maps);
    }

    @PostMapping("/save")
    public AjaxResult save(@RequestBody List<Map<String,Object>> dates)
    {
        dates.forEach(item-> {
            List<Map<String, Object>> times = (List<Map<String, Object>>) item.get("times");
            times.forEach(time -> {
                List<Map<String, Object>> clazzs = (List<Map<String, Object>>) time.get("clazzs");
                for (int i = 0; i < clazzs.size(); i++) {
                    Long dateTime = (Long)item.get("date");
                    String formatDate = DateFormatUtils.format(dateTime, DateUtils.YYYY_MM_DD);
                    String startTime = (String)time.get("startTime");
                    String endTime = (String)time.get("endTime");
                    Map<String, Object> clazz = clazzs.get(i);
                    Integer cstId = (Integer) clazz.get("cstId");
                    String uptime = formatDate+","+startTime+","+endTime;
                    Integer subid = (Integer) clazz.get("subId");
                    String subname = (String) clazz.get("subName");
                    Integer teacherid = (Integer) clazz.get("userId");
                    String teachername = (String) clazz.get("nickName");
                    Integer chapterid = (Integer) clazz.get("subChapterId");
                    String chaptername = (String) clazz.get("subChapterName");
                    Integer roomid = (Integer) clazz.get("roomId");
                    String roomname = (String) clazz.get("roomName");
                    Integer cid = (Integer) clazz.get("cid");
                    String clazzName = (String) clazz.get("clazzName");
                    ScSubjectTable scSubjectTable = new ScSubjectTable(cstId.longValue(),uptime,roomid.longValue(),subid.longValue(),subname,chapterid.longValue(),chaptername,teacherid.longValue(),teachername,roomname, cid.longValue(),clazzName);
                    scSubjectTableService.insertScSubjectTable(scSubjectTable);
                }
            });
        });
        return AjaxResult.success("success");
    }
    @PostMapping("/selectNextSubjectClazzTeacher")
    public AjaxResult selectNextSubjectClazzTeacher(@RequestBody List<Map<String,Object>> dates)
    {
        Map<Integer,Integer> nextTeacherMap = new HashMap<>(); //用于存储出现在排课过程中产生了新的课程情况
        dates.forEach(item->{
            List<Map<String,Object>> times = (List<Map<String, Object>>) item.get("times");
            times.forEach(time->{
                List<Map<String,Object>> clazzs = (List<Map<String,Object>>) time.get("clazzs");
                for (int i = 0; i < clazzs.size(); i++) {
                    Map<String,Object> clazz = clazzs.get(i);
                    clazz = scSubjectTableService.selectSubjectChapterValue(clazz);
                    Object subChapterId = clazz.get("subChapterId");//看此次是否获取到了章节编号，如果没有则需要重新计算
                    Integer clazzId = (Integer) clazz.get("clazzId");
                    if(subChapterId==null){
                        Integer clazzIdValue = nextTeacherMap.get(clazzId);
                        if(clazzIdValue==null){
                            clazzIdValue = 1;
                            nextTeacherMap.put(clazzId,1);
                        }else{
                            clazzIdValue = clazzIdValue + 1;
                            nextTeacherMap.put(clazzId,clazzIdValue);
                        }
                        clazz = scSubjectTableService.selectNextSubjectChapterNotNull(clazzIdValue, clazz);
                        System.out.println(clazz);
                    }
                    Collections.replaceAll(clazzs,clazzs.get(i),clazz);
                }
            });
        });
        return AjaxResult.success(dates);
    }

    /**
     * 查询课程列表
     */
    @PreAuthorize("@ss.hasPermi('study:subjecttable:list')")
    @GetMapping("/list")
    public TableDataInfo list(ScSubjectTable scSubjectTable)
    {
        startPage();
        List<ScSubjectTable> list = scSubjectTableService.selectScSubjectTableList(scSubjectTable);
        return getDataTable(list);
    }

    /**
     * 导出课程列表
     */
    @PreAuthorize("@ss.hasPermi('study:subjecttable:export')")
    @Log(title = "课程", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ScSubjectTable scSubjectTable)
    {
        List<ScSubjectTable> list = scSubjectTableService.selectScSubjectTableList(scSubjectTable);
        ExcelUtil<ScSubjectTable> util = new ExcelUtil<ScSubjectTable>(ScSubjectTable.class);
        util.exportExcel(response, list, "课程数据");
    }

    /**
     * 获取课程详细信息
     */
    @PreAuthorize("@ss.hasPermi('study:subjecttable:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(scSubjectTableService.selectScSubjectTableById(id));
    }

    /**
     * 新增课程
     */
    @PreAuthorize("@ss.hasPermi('study:subjecttable:add')")
    @Log(title = "课程", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ScSubjectTable scSubjectTable)
    {
        return toAjax(scSubjectTableService.insertScSubjectTable(scSubjectTable));
    }

    /**
     * 修改课程
     */
    @PreAuthorize("@ss.hasPermi('study:subjecttable:edit')")
    @Log(title = "课程", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ScSubjectTable scSubjectTable)
    {
        return toAjax(scSubjectTableService.updateScSubjectTable(scSubjectTable));
    }

    /**
     * 删除课程
     */
    @PreAuthorize("@ss.hasPermi('study:subjecttable:remove')")
    @Log(title = "课程", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(scSubjectTableService.deleteScSubjectTableByIds(ids));
    }

    public void a(){
        Map<SysUser,List<ScClazzSubjectTeacher>> map = new HashMap<>();
        map.forEach((k,v)->{

        });
        Map<String,List<ScSubjectTableVo>> map1 = new HashMap<>();
    }
}
